<?php

namespace api\controller;

use common\controller\VerifyController;
use common\service\ReportService;
use think\Request;
use think\Response;
use Throwable;

class ReportController extends VerifyController
{
    /** @var ReportService $reportService */
    private ReportService $reportService;

    public function __construct()
    {
        parent::__construct();
        $this->reportService = ReportService::getInstance();
    }

    /**
     * 生成月度报表
     * @param Request $request
     * @return Response
     * @throws Throwable
     */
    public function generate(Request $request)
    {
        $month = $request->param()['param']['month'];
        $stepCategoryName = $request->param()['param']['step_category_name'];
        $projectId = $request->param()['param']['project_id'];
        $res = $this->reportService->generate($projectId, $month, $stepCategoryName);
        return json(success_response('OK', $res));
    }

    /**
     * 工时列表
     * @param Request $request
     * @return Response
     */
    function list(Request $request) {
        $param = $request->param()['param'];
        $bigModuleCode = $param['big_module_code'];
        $filter = $param['filter'] ?? [];
        $queryType = $param['query_type'] ?? 'detail';
        $entityNameSearch = $param['entity_name_search'] ?? '';
        $page = get_default_page($param);
        $res = $this->reportService->list($request->getTenantId(), $bigModuleCode, $filter, $page, $queryType, $entityNameSearch);
        return json(success_response('OK', $res));
    }

    /**
     * 工时列表 用户汇总
     * @param Request $request
     * @return Response
     */
    public function listByUser(Request $request)
    {
        $param = $request->param()['param'];
        $bigModuleCode = $param['big_module_code'];
        $filter = $param['filter'] ?? [];
        $userNameSearch = $param['user_name_search'] ?? '';
        $page = get_default_page($param);
        $res = $this->reportService->listByUser($request->getTenantId(), $bigModuleCode, $filter, $page, $userNameSearch);
        return json(success_response('OK', $res));
    }

    /**
     * 用户月度日工时列表
     * @param Request $request
     * @return Response
     */
    public function userDayHourList(Request $request)
    {
        $param = $request->param()['param'];
        $filter = $param['filter'] ?? [];
        $res = $this->reportService->userDayHourList($request->getTenantId(), $filter);
        return json(success_response('OK', $res));
    }

    /**
     * 用户日任务工时列表
     * @param Request $request
     * @return Response
     */
    public function userDayHourTaskList(Request $request)
    {
        $param = $request->param()['param'];
        $filter = $param['filter'] ?? [];
        $page = get_default_page($param);
        $res = $this->reportService->userDayHourTaskList($request->getTenantId(), $filter, $page);
        return json(success_response('OK', $res));
    }
}
